<template>
	<view class="app-container">
		<view class="device-list">
			<view v-for="(item,index) in listDatas" :key="index" class="device-item" @click="onItemClick(item)">
				<view class="item-left">
					<image class="image" :src="item.icon" mode="aspectFill"></image>
				</view>
				<view class="item-right">
					<view class="name">
						{{item.name}} <text :style="{color:item.online?'blue':'red'}"
							class="online">{{item.online?'在线':'离线'}}</text>
					</view>
					<view class="desc">
						<text class="status" v-for="(it,ind) in item.status" :key="ind">{{ it.code }}:{{ it.value }} ；
						</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getDeviceList
	} from '@ray-js/wechat';
	export default {
		data() {
			return {
				listDatas: []
			};
		},
		onUnload() {
			uni.$off('refreshDeviceList')
		},
		onLoad() {
			this._getDeviceList()
			uni.$on('refreshDeviceList', val => {
				this._getDeviceList()
			})
		},
		methods: {
			onItemClick(item) {
				const {
					id,
					category
				} = item
				switch (category) {
					case 'qt': //面膜仪
						uni.navigateTo({
							url: `/pages/bleota/index?deviceId=${id}`
						})
						break;
					default:
						uni.showToast({
							icon: 'error',
							title: '非法设备'
						})
						break
				}
			},
			//获取已经联网数据信息
			async _getDeviceList() {
				const response = await getDeviceList()
				if (response) {
					this.listDatas = [...response]
					console.log(this.listDatas);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.app-container {
		.device-list {
			.device-item {
				background-color: #ffffff;
				border-radius: 10upx;
				margin: 20upx 20upx 0;
				display: flex;
				align-items: center;

				.item-left {
					.image {
						width: 150upx;
						height: 150upx;
					}
				}

				.item-right {
					margin-left: 20upx;
					min-height: 120upx;
					padding-top: 20upx;

					.name {
						font-size: 34upx;
						font-weight: 700;

						.online {
							font-weight: normal;
							margin-left: 10upx;
							font-size: 24upx;
						}
					}

					.desc {
						margin-top: 20upx;
						font-size: 28upx;
						display: flex;
						flex-wrap: wrap;

						.status {
							margin-right: 20upx;
						}
					}
				}
			}
		}
	}
</style>
<style>
	page {
		background-color: #f1f1f1;
	}
</style>